package com.newcapec.servlet;

import com.newcapec.bean.NewsEntity;
import com.newcapec.bean.UserEntity;
import com.newcapec.service.NewsService;
import com.newcapec.service.SysUserService;
import com.newcapec.service.impl.NewsServiceImpl;
import com.newcapec.service.impl.SysUserServiceImpl;
import com.newcapec.util.MD5Util;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.io.InputStream;

/**
 * @Description:修改新闻信息的servlet
 * @Author:yangshupeng
 * @Date: 2022/3/20 21:19
 * @Version:1.0
 */
@WebServlet("/editNews")
/*@MultipartConfig*/
public class UpdateNewsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");*/

        String id = req.getParameter("id");

        String newsTitle = req.getParameter("newsTitle");
        String newsDescr = req.getParameter("newsDescr");
        String newsKeyword = req.getParameter("newsKeyword");
        String newsSource = req.getParameter("newsSource");
        String columnId = req.getParameter("columnId");
        String isTop = req.getParameter("isTop");
        //获取上传的文件
        /*Part mainImg = req.getPart("upload");
        String imgName = mainImg.getSubmittedFileName();*/
        /*String localPath = "D://upload/";
        //将上传的文件保存到指定的路径
        mainImg.write(localPath + imgName);*/

        String newsContent = req.getParameter("newsContent");
        String pageNum = req.getParameter("pageNum");
        //获取操作人的姓名
        UserEntity user = (UserEntity) req.getSession().getAttribute("user");

        NewsService newsService = new NewsServiceImpl();

        NewsEntity newsEntity = new NewsEntity();
        if ((newsContent == null || newsContent.equals("")) && id != null){
            NewsEntity newsEntity1 = newsService.queryById(id);
            System.out.println(newsEntity1.getDetail());
            newsEntity.setDetail(newsEntity1.getDetail());

        }else {

            newsEntity.setDetail(newsContent);
        }
        if (id != null && newsTitle != null && columnId != null && newsContent != null && newsDescr != null && newsKeyword != null && newsSource != null && isTop != null) {
            newsEntity.setId(Integer.parseInt(id));
            newsEntity.setTitle(newsTitle);
            newsEntity.setDescr(newsDescr);
            newsEntity.setKeyword(newsKeyword);
            newsEntity.setSource(newsSource);
            newsEntity.setTop(Integer.parseInt(isTop));
            newsEntity.setChnlcode(Integer.parseInt(columnId));
            /*newsEntity.setMainImg(imgName);*/
            newsEntity.setOptor(user.getName());
            newsEntity.setState(1);
        }else {
            resp.getWriter().write("no");
        }


        if (newsService.updateNews(newsEntity)){
            req.getSession().setAttribute("pageNum",pageNum);
            resp.sendRedirect("NewsManagement.html");

        }else {
            resp.getWriter().write("no");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
